
use crate::ray_display::AsyncCalScreen;
use raylib::color::Color;
use raylib::drawing::RaylibDraw;
use raylib::init;
use raylib::prelude::KeyboardKey::{KEY_DOWN, KEY_UP};
use raylib::prelude::{RaylibFont, Vector2};
use std::fs::File;
use std::io::{Read, Write};
use std::sync::atomic::AtomicBool;
use std::sync::{Arc, RwLock};


mod nonblock_io;
mod ray_display;

fn main() {
    // let mut file = File::open("D:/RustProject/raylib-font/src/main.rs").unwrap();
    let mut file = File::open("D:/abc.log").unwrap();
    let mut file_data = Vec::new();
    file.read_to_end(&mut file_data).unwrap();
    let string = String::from_utf8(file_data).unwrap();
    test3(string.as_str());
}

fn test3(text: &str) {
    let mut builder = init();
    let (mut handle, thread) = builder.width(1500).height(1500).build();
    let async_cal_screen = AsyncCalScreen {
        cal_screen_string: Arc::new(RwLock::new(Some(text.to_owned()))),
        cache_out: Arc::new(Default::default()),
        cache_ready_flag: AtomicBool::new(false),
    };
    let arc_async_cal_screen = Arc::new(async_cal_screen);
    arc_async_cal_screen.clone().start_manager_thread();

    let mut display_cache = Vec::new();
    let mut output = Vec::new();

    let mut frame_counts = 0;

    handle.set_target_fps(60);
    let mut init_display_pos = Vector2::new(0., 0.);
    let mut keydown = false;
    let mut keyup = false;

    while !handle.window_should_close() {
        frame_counts += 1;
        match arc_async_cal_screen.clone().safe_get_cache_out() {
            None => {
                output = display_cache.clone();
                if output.is_empty() {
                    let mut prompt = "loading".to_owned();
                    let point = vec!["."; frame_counts % 60].join(" ");
                    prompt.push_str(point.as_str());
                    output = vec![prompt];
                }
            }
            Some(c) => {
                display_cache.extend_from_slice(&c);
                output = display_cache.clone();
            }
        }


        let mut draw_handle = handle.begin_drawing(&thread);
        draw_handle.clear_background(Color::BLACK);

        if draw_handle.is_key_pressed(KEY_DOWN) {
            keydown  = true
        }
        if draw_handle.is_key_released(KEY_DOWN) {
            keydown = false
        }
        if draw_handle.is_key_pressed(KEY_UP) {
            keyup  = true
        }
        if draw_handle.is_key_released(KEY_UP) {
            keyup = false
        }
        if keydown {
            init_display_pos.y-=35.;
        }
        if keyup {
            init_display_pos.y+=35.
        }
        let mut display_pos = init_display_pos.clone();

        for i in output.iter() {
            draw_handle.draw_text_ex(
                draw_handle.get_font_default(),
                i.as_str(),
                display_pos,
                35.,
                3.,
                Color::WHITE,
            );
            display_pos.y += 35.;
        }
    }
}
